package codegen;

import db.domain.User;
import javassist.ClassPool;
import org.junit.jupiter.api.Test;
import org.smallframework.mavenplugin.goal.EnhanceASMMojo;
import sf.codegen.EnhanceTaskJavassist;
import sf.codegen.EntityEnhancerJavassist;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class EnJavassist {
    public static void main(String[] args) throws Exception {
        EnhanceTaskJavassist.genPushPull = true;
        new EntityEnhancerJavassist().enhance("db.domain");
    }

    @Test
    public void t4() {
        User u = new User();
    }

    @Test
    public void t1() {
        EnhanceASMMojo em = new EnhanceASMMojo();
        em.setPath("E:/SoftwareProject/STSMyProject/SormGit/sorm-test/target/classes");
        em.execute();
    }

    @Test
    public void t2() throws Exception {
        EnhanceTaskJavassist e = new EnhanceTaskJavassist();
        ClassLoader cl = new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                Class clazz = null;
                String classFilename = name + ".class";
                File classFile = new File(classFilename);
                if (classFile.exists()) {
                    try (FileChannel fileChannel = new FileInputStream(classFile)
                            .getChannel();) {
                        MappedByteBuffer mappedByteBuffer = fileChannel
                                .map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
                        byte[] b = mappedByteBuffer.array();
                        clazz = defineClass(name, b, 0, b.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (clazz == null) {
                    throw new ClassNotFoundException(name);
                }
                return clazz;
            }
        };
        ClassPool pool = ClassPool.getDefault();
        e.doEnhance(pool, pool.get("db.domain.User"));
    }

    @Test
    public void t3() throws Exception {
        EnhanceTaskJavassistA e = new EnhanceTaskJavassistA();
        ClassLoader cl = new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                Class clazz = null;
                String classFilename = name + ".class";
                File classFile = new File(classFilename);
                if (classFile.exists()) {
                    try (FileChannel fileChannel = new FileInputStream(classFile)
                            .getChannel();) {
                        MappedByteBuffer mappedByteBuffer = fileChannel
                                .map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
                        byte[] b = mappedByteBuffer.array();
                        clazz = defineClass(name, b, 0, b.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (clazz == null) {
                    throw new ClassNotFoundException(name);
                }
                return clazz;
            }
        };
        ClassPool pool = ClassPool.getDefault();
        e.doEnhance(pool, "db.domain.User");
    }
}
